import { createRouter, createWebHistory, createWebHashHistory, useRoute, useRouter } from 'vue-router'
import storageUtil from "../util/storageUtil";
import { store } from "../store";
import useUserEffect from "../hooks/useUserEffect";
import { getPathWxMpAppId } from "/@/util";

let routes = [
    {
        path: '/',
        name: 'index',
        meta: {
            title: 'Index'
        },
        redirect: '/my',
        component: () => import('../views/index.vue'),
    },
    {
        path: '/login',
        name: 'login',
        meta: {
            title: '360问卷',
            noAuth: true,
            noBind: true,
            noSelectStore: true,
        },
        component: () => import('../views/login.vue'),
    },
    {
        path: '/my',
        meta: { title: '', },
        component: () => import('../views/my/index.vue'),
    },
    {
        path: '/examHome',
        meta: { title: '', noAuth: true, },
        component: () => import('../views/exam/home.vue'),
    },
    {
        path: '/welcome',
        meta: { title: '', noAuth: true, },
        component: () => import('../views/exam/welcome.vue'),
    },
    {
        path: '/remind',
        meta: { title: '', noAuth: true, },
        component: () => import('../views/exam/remind.vue'),
    },
    {
        path: '/examList',
        meta: { title: '试卷列表', noAuth: true, },
        component: () => import('../views/exam/list.vue'),
    },
    {
        path: '/examDetail',
        meta: { title: '详情', noAuth: true, },
        component: () => import('../views/exam/detail.vue'),
    },
    {
        path: '/examine',
        meta: { title: '问卷调查', noAuth: true, },
        component: () => import('../views/examine/index.vue'),
    },
    {
        path: '/exam',
        meta: { title: '问卷调查', noAuth: true, },
        component: () => import('../views/exam/index.vue'),
    },
    {
        path: '/employee',
        meta: { title: '人员列表', noAuth: true, },
        component: () => import('../views/employee/index.vue'),
    },
    {
        path: '/report',
        meta: { title: '报表' },
        component: () => import('../views/report/index.vue'),
    },
    {
        path: '/reportHome',
        meta: { title: '报表' },
        component: () => import('../views/report/home.vue'),
    },
    {
        path: '/reportActivity',
        meta: { title: '活动报表' },
        component: () => import('../views/report/components/Activity.vue'),
    },
    {
        path: '/reportEmployee',
        meta: { title: '人员报表' },
        component: () => import('../views/report/components/Employee.vue'),
    },
    {
        path: '/reportEmployeeDetail',
        meta: { title: '人员报表详情' },
        component: () => import('../views/report/employee/detail.vue'),
    },
    {
        path: '/reportEmployeeList',
        meta: { title: '人员报表详情' },
        component: () => import('../views/report/employee/list.vue'),
    },
    {
        path: '/reportActivityDetail',
        meta: { title: '人员活动详情' },
        component: () => import('../views/report/activity/detail.vue'),
    },


]

const router = createRouter({
    // history: createWebHistory(`/coupon/${getPathWxMpAppId()}`), // hash模式：createWebHashHistory，history模式：createWebHistory
    // history: createWebHistory(`/${getPathWxMpAppId()}`), // hash模式：createWebHashHistory，history模式：createWebHistory
    history: createWebHashHistory('/'), // hash模式：createWebHashHistory，history模式：createWebHistory

    routes: routes.map(r => {
        // r.path = `/:wxMpAppId${r.path}`
        return r
    }) as any
})

router.beforeEach((async (to: any, from, next) => {
    const title = to.meta && to.meta.title
    console.warn('当前路由=========', to.fullPath, from.fullPath, to.meta)
    if (title) {
        document.title = title;
    }
    //是否需要授权
    const hasAuth = !to.meta.noAuth
    //是否需要绑定客户
    // const hasBind = !to.meta.noBind



    if (to.query.__systemCode) {
        storageUtil.setSystemCode(to.query.__systemCode)
        await store.dispatch("systems/getSystemInfoSync")
    }


    //不需要登录的
    if (!hasAuth) {
        return next()
    }
    //token登录后才有
    // const token = storageUtil.getCustomerAuthorization()
    //没有token 去登录
    // if (!token) {
    //     console.log('login=====33333333')
    //     return next({
    //         path: '/login',
    //         query: { redirect: encodeURIComponent(to.fullPath) }
    //     })
    // }
    // let { isBinding, getSystemCode } = useUserEffect()
    //查看用户信息 没有再拉取
    const userDingDTO = store.state.users.userDingDTO
    console.log('WX_MP_USER_DTO======', userDingDTO)

    if (!userDingDTO || !userDingDTO.userid) {
        return next({
            path: '/login',
            query: { redirect: encodeURIComponent(to.fullPath) }  // 将跳转的路由path作为参数，登录成功后跳转到该路由
        })
        // try {
        //     console.log('GET_USER_INFO-------1111111')
        //     // await store.dispatch('users/getUserInfo')
        // } catch (e) {
        //     console.error('redirect=========home')
        //     console.log('login=====444444444')
        //     return next({
        //         path: '/login',
        //         query: { redirect: encodeURIComponent(to.fullPath) }  // 将跳转的路由path作为参数，登录成功后跳转到该路由
        //     })
        // }
    }


    //正常通过
    next()
}))

export default router
